﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TpAgentCore.Base;
using TpTools;
using TpLogger;
using TpBaseObjects.Objects;
using TpData.Manager;
using TpContracts;

namespace TpAgent
{
	public class Program {

        #region Members

        private const string errType = "Err";
        private const string logType = "Log";
        private const string logArea = "TpAgent.Programm";

        #endregion

        #region Main

        [STAThread]
		static void Main(string[] args)
		{
            TecUserObj u = TecUserDataManager.LoadTecUser(Environment.UserName);
            Contracts.SetMyUser(u);
			LogDataManager.SaveLog(Contracts.MyUser.UserID, logType, logArea, "Start Agent", false);
            
            try {
                Agent myCore = new Agent();
                if (args == null | args.Length == 0) {
                    myCore.Run();
                } else {
                    foreach (string runNo in args) {
                        int CallFunctionNumber = 0;
                        bool result = Int32.TryParse(runNo, out CallFunctionNumber);
                        if (result) {
                            Agent.CallNumber callNo = (Agent.CallNumber)Math.Abs(CallFunctionNumber);
                            myCore.Run(callNo);
                        }

                    }
                }
                myCore = null;
            } catch (Exception ex) {
                LogDataManager.SaveLog(Contracts.MyUser.UserID, errType, logArea, "Main: " + ex.Message, true);
            } finally {
                LogDataManager.SaveLog(Contracts.MyUser.UserID, logType, logArea, "End Agent", false);
            }
        }

        #endregion

    }


}
